package com.cq.jackmar.rxdemo.demo;

import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.graphics.PointF;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.cq.jackmar.rxdemo.R;
import com.cq.jackmar.rxdemo.base.BaseActivity;
import com.cq.jackmar.rxdemo.view.rvlist.RefreshUtil;
import com.cq.jackmar.rxdemo.view.rvlist.view.JRecyclerView;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by JackMar on 2017/3/13.
 * 邮箱：1261404794@qq.com
 */

public class BeizerActivity extends BaseActivity {
    @Bind(R.id.recyclerView)
    JRecyclerView mRecyclerView;
    @Bind(R.id.iv_image)
    ImageView mIvImage;
    private List<String> strings = new ArrayList<>();
    private CommonAdapter<String> mAdapter;

    private PointF Start = new PointF(0, 0);
    private PointF End = new PointF(0, 1920 * 0.98f);
    private PointF control = new PointF(0, 0);
    private RefreshUtil refreshUtil;
    private static final String TAG = "BeizerActivity";


    @Override
    protected int getLayoutId() {
        return R.layout.activity_beizer;
    }

    @Override
    protected void initView() {
        for (int i = 0; i < 50; i++) {
            strings.add(" ");
        }
        refreshUtil = new RefreshUtil(context, mRecyclerView);
        refreshUtil.enableLoad(false);
        refreshUtil.enableRefresh(false);
        mAdapter = new CommonAdapter<String>(context, R.layout.cell_beizer, strings) {
            @Override
            protected void convert(final ViewHolder holder, String s, final int position) {
                holder.setOnClickListener(R.id.iv_add, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
                        int firstPosition = linearLayoutManager.findFirstCompletelyVisibleItemPosition();
                        View firstItem = linearLayoutManager.findViewByPosition(firstPosition);
                        int[] location = new int[2];
                        holder.getView(R.id.iv_add).getLocationOnScreen(location);
                        Start.x = location[0];
                        Start.y = location[1];
                        Log.e(TAG, "onClick: " + Start.y);
                        Log.e(TAG, "onClick: " + firstItem.getHeight());
                        mIvImage.setX(Start.x);
                        mIvImage.setY(Start.y);
                        Start.y = location[1];
                        mIvImage.setVisibility(View.VISIBLE);
                        beizer(mIvImage);
                    }
                });
            }
        };
        mRecyclerView.setAdapter(mAdapter);
    }

    private void beizer(final ImageView imageView) {
        BezierEvalutor evalutor = new BezierEvalutor(control);
        ValueAnimator valueAnimator = ValueAnimator.ofObject(evalutor, Start,
                End);
        valueAnimator
                .addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        // 获取从估值器中计算后的坐标
                        PointF pointF = (PointF) animation.getAnimatedValue();
                        imageView.setX(pointF.x);
                        imageView.setY(pointF.y);
                        // 设置图片的透明度，让图片由1渐变到0透明度,getFraction()相当于动画的进度
//                        imageView.setAlpha(1 - animation.getAnimatedFraction());
                    }
                });
        valueAnimator.setTarget(imageView);
        valueAnimator.setDuration(2000);
        valueAnimator.start();
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    /**
     * 自定义一个估值器实现起始点到终止点的过程中坐标的变化
     */
    public class BezierEvalutor implements TypeEvaluator<PointF> {

        PointF control1Point;

        public BezierEvalutor(PointF control1Point) {
            super();
            this.control1Point = control1Point;
        }

        @Override
        public PointF evaluate(float t, PointF start, PointF end) {
            // 时间因子t: 0~1
            PointF point = new PointF();
            // 实现贝塞尔公式:
            point.x = start.x * (1 - t) * (1 - t) + 2 * t * (1 - t) * control1Point.x + t * t * end.x;
//                    * control2Point.x * (1 - t) * t * t + end.x * t * t * t;// 实时计算最新的点X坐标
            point.y = start.y * (1 - t) * (1 - t) + 2 * t * (1 - t) * control1Point.y + t * t * end.y;
            return point;// 实时返回最新计算出的点对象
        }

    }
}
